/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import ohos.agp.components.Component;

/**
 * ZoomOutSlideTransformer
 *
 * @since 2021-08-06
 */
public class ZoomOutSlideTransformer implements BannerViewPager.PageTransformer {
    private static final float NUM_F_0_85 = 0.85f;
    private static final float NUM_F_0_5 = 0.5f;
    private static final float NUM_F_0_149 = 0.14999998f;
    private static final float NUM_F_2 = 2f;
    float scaleFactor = 0F;
    float vertMargin = 0F;
    float horzMargin = 0F;

    /**
     * transformerPage
     *
     * @param pageType     BannerViewPager.PageType
     * @param page         Component
     * @param offset       float
     * @param offsetPixels float
     */
    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        float height = (float) page.getHeight();
        float width = (float) page.getWidth();
        switch (pageType) {
            case PREVIOUS:
                float previousFloat = 1 - offset;
                if (offsetPixels < 0) {
                    scaleFactor = Math.max(NUM_F_0_85, (float) 1 - Math.abs(previousFloat));
                    vertMargin = height * ((float) 1 - scaleFactor) / NUM_F_2;
                    horzMargin = width * ((float) 1 - scaleFactor) / NUM_F_2;
                    page.setPivotY(NUM_F_0_5 * height);
                    page.setPivotX(NUM_F_0_5 * width);
                    page.setTranslationX(horzMargin - vertMargin / NUM_F_2);
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                    page.setAlpha(NUM_F_0_5 + (scaleFactor - NUM_F_0_85) / NUM_F_0_149 * NUM_F_0_5);
                } else {
                    page.setAlpha(1F);
                    page.setTranslationX(1F);
                }
                break;
            case CURRENT:
                scaleFactor = Math.max(NUM_F_0_85, (float) 1 - Math.abs(offset));
                vertMargin = height * ((float) 1 - scaleFactor) / NUM_F_2;
                horzMargin = width * ((float) 1 - scaleFactor) / NUM_F_2;
                page.setPivotY(NUM_F_0_5 * height);
                page.setPivotX(NUM_F_0_5 * width);
                if (offsetPixels > 0) {
                    page.setTranslationX(horzMargin - vertMargin / NUM_F_2);
                } else {
                    page.setTranslationX(-horzMargin + vertMargin / NUM_F_2);
                }
                page.setScaleX(scaleFactor);
                page.setScaleY(scaleFactor);
                page.setAlpha(NUM_F_0_5 + (scaleFactor - NUM_F_0_85) / NUM_F_0_149 * NUM_F_0_5);
                break;
            case NEXT:
                float nextFloat = offset - 1;
                if (offsetPixels >= 0) {
                    scaleFactor = Math.max(NUM_F_0_85, (float) 1 - Math.abs(nextFloat));
                    vertMargin = height * ((float) 1 - scaleFactor) / NUM_F_2;
                    horzMargin = width * ((float) 1 - scaleFactor) / NUM_F_2;
                    page.setPivotY(NUM_F_0_5 * height);
                    page.setPivotX(NUM_F_0_5 * width);
                    page.setTranslationX(-horzMargin + vertMargin / NUM_F_2);
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                    page.setAlpha(NUM_F_0_5 + (scaleFactor - NUM_F_0_85) / NUM_F_0_149 * NUM_F_0_5);
                } else {
                    page.setAlpha(1F);
                    page.setTranslationX(1F);
                }
                break;
            case OTHER:
                page.setAlpha(1F);
                page.setTranslationX(1F);
                break;
            default:
                break;
        }
    }
}